package com.cenxi.controller;

import com.cenxi.common.dto.RoleDTO;
import com.cenxi.common.result.PageReq;
import com.cenxi.common.result.PageResp;
import com.cenxi.common.result.Req;
import com.cenxi.common.result.Resp;
import com.cenxi.common.vo.RoleVO;
import com.cenxi.dao.manage.ManageRoleService;
import com.cenxi.utils.constants.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @Author cenxi
 * @Date 2023/11/14 14:54
 * @Version 1.0
 */

@RestController
@RequestMapping("/role")
@Api(tags = "角色接口")
public class RoleController {

    @Autowired
    private ManageRoleService manageRoleService;

    @PostMapping("/list")
    @ApiOperation(value = "获取角色选择列表")
    public Resp<List<RoleVO>> list() {
        return manageRoleService.list();
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取平台角色分页列表")
    public PageResp<RoleVO> page(@RequestBody PageReq<RoleDTO> req) {
        return manageRoleService.page(req);
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建角色")
    public Resp<Boolean> create(@RequestBody Req<RoleDTO> req) {
        return manageRoleService.create(req);
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改角色")
    public Resp<Boolean> edit(@RequestBody Req<RoleDTO> req) {
        return manageRoleService.edit(req);
    }

    @PostMapping("/findById")
    @ApiOperation(value = "根据id查询平台角色信息")
    public Resp<RoleVO> findById(@RequestBody Req<Long> req) {
        return manageRoleService.findById(req);
    }

    @PostMapping("/enable")
    @ApiOperation(value = "启用角色")
    public Resp<Boolean> enable(@RequestBody Req<List<Long>> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageRoleService.enable(req.getData(), Constants.ENABLE);
    }

    @PostMapping("/disable")
    @ApiOperation(value = "禁用角色")
    public Resp<Boolean> disable(@RequestBody Req<List<Long>> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageRoleService.disable(req.getData(), Constants.DISABLE);
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "根据id删除平台角色信息")
    public Resp<Boolean> deleteById(@RequestBody Req<List<Long>> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageRoleService.deleteById(req.getData());
    }
}
